package com.quartz.springbootquartz.web;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @description quartz的web接口层
 * @Author longdeng
 * @Date 2020/7/27 0027 22:55
 */
@Controller
@RequestMapping("/api/quartz")
public class QuartController {


    @Autowired
    private QuartzService quartzService;

    /**
     * 启动定时调度任务
     *
     * @return
     */
    @GetMapping("/startQuartz")
    @ResponseBody
    public Map<String, Object> startQuartz() throws SchedulerException {
        Map<String, Object> map = quartzService.start();
        return map;
    }

    /**
     * 停止定时调度任务
     *
     * @param name
     * @param group
     * @return
     */
    @GetMapping("/parseJob")
    @ResponseBody
    public Map<String, Object> parseJob(@RequestParam("name") String name,
                                        @RequestParam("group") String group) throws SchedulerException {
        Map<String, Object> map = quartzService.parseJob(name, group);
        return map;
    }

    /**
     * 停止定时调度任务
     *
     * @param name
     * @param group
     * @return
     */
    @GetMapping("/startJob")
    @ResponseBody
    public Map<String, Object> startJob(@RequestParam("name") String name,
                                        @RequestParam("group") String group) throws SchedulerException {
        return quartzService.startJob(name, group);
    }

    /**
     * 删除定时调度任务
     *
     * @param name
     * @param group
     * @return
     */
    @GetMapping("/deleteJob")
    @ResponseBody
    public Map<String, Object> deleteJob(@RequestParam("name") String name,
                                         @RequestParam("group") String group) throws SchedulerException {
        return quartzService.deleteJob(name, group);
    }


    /**
     * 添加定时调度任务
     *
     * @param name
     * @param group
     * @return
     */
    @GetMapping("/addJob")
    @ResponseBody
    public Map<String, Object> addJob(@RequestParam("name") String name,
                                      @RequestParam("group") String group) throws SchedulerException {

        return quartzService.addJob(name, group);
    }

}
